﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABELGX
{
    public class DriverItem
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 离线
        /// </summary>
        public RegisterBool Offline { get; set; }
        /// <summary>
        /// 过流
        /// </summary>
        public RegisterBool CurrentOver { get; set; }
        /// <summary>
        /// 过压
        /// </summary>
        public RegisterBool VoltageOver { get; set; }
        /// <summary>
        /// 欠压
        /// </summary>
        public RegisterBool VoltageUnder { get; set; }
        /// <summary>
        /// 过温
        /// </summary>
        public RegisterBool TemperatureOver { get; set; }
        /// <summary>
        /// 速度
        /// </summary>
        public Sensor Speed { get; set; }
        /// <summary>
        /// 告警
        /// </summary>
        public bool Warning => Offline.Value || CurrentOver.Value || VoltageOver.Value || VoltageUnder.Value || TemperatureOver.Value;
        public DateTime WarningTime { get; set; }
        public DriverItem(string name)
        {
            Name = name;
            Offline = new RegisterBool(new CustomizeSet(0, true, "", "故障", "正常", "Red", "Lime"), false);
            CurrentOver = new RegisterBool(new CustomizeSet(0, true, "", "过流", "正常", "Red", "Lime"), false);
            VoltageOver = new RegisterBool(new CustomizeSet(0, true, "", "过压", "正常", "Red", "Lime"), false);
            VoltageUnder = new RegisterBool(new CustomizeSet(0, true, "", "欠压", "正常", "Red", "Lime"), false);
            TemperatureOver = new RegisterBool(new CustomizeSet(0, true, "", "过温", "正常", "Red", "Lime"), false);
            Speed = new Sensor(new SensorSet("", "m/s", 0.01, 0, "f2"));
        }
        public void SetInfo(byte info)
        {
            Offline.Value = (info & 0x01) != 0;
            CurrentOver.Value = (info & 0x02) != 0;
            VoltageOver.Value = (info & 0x04) != 0;
            VoltageUnder.Value = (info & 0x08) != 0;
            TemperatureOver.Value = (info & 0x10) != 0;
            SaveWarningLog();
        }
        public void SaveWarningLog()
        {
            if (!Warning)
            {
                return;
            }
            WarningTime = DateTime.Now;
            if (Offline.Value)
            {
                Config.WriteWarning($"{Name},离线");
            }
            if (CurrentOver.Value)
            {
                Config.WriteWarning($"{Name},过流");
            }
            if (VoltageOver.Value)
            {
                Config.WriteWarning($"{Name},过压");
            }
            if (VoltageUnder.Value)
            {
                Config.WriteWarning($"{Name},欠压");
            }
            if (TemperatureOver.Value)
            {
                Config.WriteWarning($"{Name},过热");
            }
        }

    }
}
